import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core';
import { CommService } from './../../comm/comm.service';
import { Http, Response } from '@angular/http';
import { Router } from '@angular/router';
import { FormsModule } from '@angular/forms';
import 'rxjs/add/operator/map';
import { Subject } from 'rxjs/Rx';
import { Observable } from 'rxjs/Rx';
import { NgForm } from '@angular/forms';
import { ReportUrls } from '../../comm/report.urls.conf';
import { SubArea } from '../../model/report/sub-area';
import { QueryModel } from '../../model/report/query-model';
import { ResponseModel } from '../../model/comm/response-model';
import { FileUploader } from 'ng2-file-upload';
declare const $: any;
import * as moment from 'moment';
import { TVResource } from 'app/ptpmc/model/comm/tv-resource';
import { DxDataGridComponent } from '../../../../../node_modules/devextreme-angular';
declare var swal: any;

@Component({
    selector: 'app-fault-word-description',
    templateUrl: './fault-word-description.component.html',
    styleUrls: ['./fault-word-description.component.css'],
})

export class FaultWordDescriptionComponent implements OnInit, AfterViewInit {

    @ViewChild(DxDataGridComponent) dataGrid: DxDataGridComponent;
    //excel 文件导入
    public uploader: FileUploader = new FileUploader({
        url: ReportUrls.BaseUrl + ReportUrls.FaultWorldDescriptionImport,
        method: 'POST',
        autoUpload: false,
        itemAlias: 'uploader',
    });
    /**查询按类型 */
    // public typeItems = [{ id: 1, text: '按值统计' }, { id: 2, text: '按班次统计' }];
    // public typeSelected = this.typeItems[0];


    /*表示设置目标值的Dialog是否打开*/
    public opened = false;
    //查询区
    public queryModel: any = {};
    /**选择项 */
    public items: any[] = [];
    public selectedItems: any[] = [];


    /*生产线 - 班次*/
    public subAreaItems: SubArea[];
    public equipemntItems: TVResource[] = [];

    public subAareSelected: SubArea;
    public equipmentSelected: TVResource;

    constructor(private commServices: CommService) {
    }
    onSubAreaSelectedChange(): void {
        this.equipemntItems = this.commServices.getTVResourceBySubArea(this.subAareSelected.subArea);
        this.equipmentSelected = this.equipemntItems[0];

    }

    ngOnInit() {
        this.subAreaItems = this.commServices.getSubAreas();
        this.subAareSelected = this.subAreaItems[0];
        this.equipemntItems = this.commServices.getTVResourceBySubArea(this.subAareSelected.subArea);
        this.equipmentSelected = this.equipemntItems[0];
        this.getReportData();
    }

    ngAfterViewInit() {
    }


    downloadTemplate() {
        window.open(ReportUrls.BaseUrl + ReportUrls.FaultWorldDescriptionTempExport);
    }
    /**获取表格数据*/

    getReportData(): void {
        const url: string = ReportUrls.FaultWorldDescription;
        const fwd = { subarea: this.equipmentSelected.subArea, resource: this.equipmentSelected.resource };
        this.commServices
            .getList(fwd, url)
            .subscribe(model => {
                this.items = [];
                if (model.resultCode === 200) {
                    this.items = model.data;
                } else {
                    console.log(model.resultCode + ',' + model.resultMessage);
                }
            });
    }

    /**获取TimeRange的值*/
    getTime(id: any): String {
        const timerange: String = $('#' + id).val();
        return timerange;
    }
    // openExcelImportDialog() {
    //     $('#excleImportModal').modal('toggle');
    // }

    importExcel() {
        // 上传
        this.uploader.queue[0].onSuccess = (response, status, headers) => {
            // 上传文件成功
            if (status === 200) {
                // 上传文件后获取服务器返回的数据
                const res = JSON.parse(response);
                if (res.resultCode === 200) {
                    this.getReportData();
                } else {
                    alert('数据导入出错！');
                }
                this.uploader.clearQueue();
            } else {
                // 上传文件后获取服务器返回的数据错误
                alert('数据导入失败');
            }
        };
        this.uploader.setOptions({ additionalParameter: this.equipmentSelected });
        this.uploader.queue[0].upload(); // 开始上传   

    }

}
